﻿using SqlSugar.Extensions;

namespace DataAcquisition
{
    public partial class FormPointAddForm : BaseForm
    {
        #region 属性

        private FormPoint obj = new FormPoint();

        /// <summary>设备点位
        /// 
        /// </summary>
        public DevicePointItem? devicePointObj;

        #endregion

        #region 自定义方法

        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            obj = new FormPoint();
            devicePointObj = null;
            FormPointNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            FormPointNameInput.Text = "";
            DevicePointNameInput.Text = "";
        }
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["name"] = FormPointNameInput.Text;
            List<FormPoint> list = SqlHelper.Inquireable<FormPoint>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("已存在【{0}】点位", where["name"]);
                Error(msg);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>判断设备是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckDevicePointExistWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<DevicePoint> list = SqlHelper.Inquireable<DevicePoint>(where);

            if (list.Count == 0)
            {
                string msg = string.Format("【{0}】编号设备点位不存在", number);
                Error(msg);
                return false;
            }
            else
            {
                DevicePoint devicePoint = list[0];
                if (devicePoint.Name is not null)
                {
                    DevicePointNameInput.Text = devicePoint.Name;
                    obj.DevicePointNumber = devicePoint.Number;
                    obj.DevicePointName = devicePoint.Name;
                }
                return true;
            }
        }

        /// <summary>刷新设备点位信息
        /// 
        /// </summary>
        public void ReloadDevicePointInfo()
        {
            if (devicePointObj is not null)
            {
                CheckDevicePointExistWithNumber(devicePointObj.Number);
                DevicePointNameInput.Enabled = true;
            }
            else
            {
                string msg = string.Format("设备点位不存在");
                Error(msg);
            }

        }
        #endregion

        #region 系统方法
        public FormPointAddForm()
        {
            InitializeComponent();
            Header.Text = "添加界面点位";
        }

        private void FormPointAddForm_Load(object sender, EventArgs e)
        {
            Reload();
        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (FormPointNameInput.Text.Length == 0)
            {
                Error("请输入界面点位名称");
                FormPointNameInput.Focus();
                return;
            }
            obj.Name = FormPointNameInput.Text;

            if (devicePointObj == null)
            {
                Error("请选择设备点位");
                return;
            }

            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            obj.Number = FormPointNumberInput.Text.ObjToInt();
            List<FormPoint> list = new List<FormPoint>();
            list.Add(obj);
            int res = SqlHelper.Insertable(list);
            if (res > 0)
            {
                Reload();
                if (this.Owner is not null)
                {
                    FormPointManageForm frm = (FormPointManageForm)Owner;
                    frm.LoadList();
                }
                this.Close();
            }
            else
            {
                Error("添加点位失败");
            }
        }
        private void DevicePointNameInput_Click(object sender, EventArgs e)
        {
            DevicePointNameInput.Enabled = false;
            DevicePointSelectForm frm = new DevicePointSelectForm();
            frm.Owner = this;
            frm.ShowDialog();
        }
        #endregion
    }
}
